//
//  DYMSDaiquerenViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/6.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYMSDaiquerenViewController: SYBaseViewController , UITableViewDelegate, UITableViewDataSource{
    
    
    var bottom: DYDaifukuanBottom!
    var model: DYMSDaiquerenModel!
    let topBg: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "meishi_daiqueren_bg")
        v.isUserInteractionEnabled = true
        return v
    }()
    
    let statusLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 18.autoBoldFontSize()
        
        return v
    }()
    
    let shuomingLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 12.autoFontSize()
        v.numberOfLines = 0
        
        return v
    }()
    
    let yingye: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 12.autoFontSize()
        v.text = "等待商家营业中"
        return v
    }()
    
    let chuli: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 12.autoFontSize()
        v.text = "商家处理中"
        return v
    }()
    
    let queren: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 12.autoFontSize()
        v.text = "结果确认"
        return v
    }()
    let yingyeBg: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 6.autoWidth()
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor(hex: 0xFF8A43)
        return v
    }()
    let chuliBg: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 6.autoWidth()
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor(hex: 0xFF8A43)
        return v
    }()
    let querenBg: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 6.autoWidth()
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor(hex: 0xFF8A43)
        return v
    }()
    let yingyeImg: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_jiedian")
        return v
    }()
    let chuliImg: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_jiedian")
        return v
    }()
    let querenImg: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "meishi_jiedian")
        return v
    }()
    var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xFF8A43)
        return v
    }()
    
    var back: UIButton = {
        let v: UIButton = UIButton()
        v.setImage(UIImage(named: "nav_back"), for: .normal)
        return v
    }()
    
    
    var tableView: UITableView!
    
    var id: String!
    
    init(id: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTopView()
        setTableView()
        
        self.view.backgroundColor = Constant.bgViewColor
        self.getData(id)
    }
    
    func getData(_ id: String) {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_meishiOrderDetail(id: id), modelClass: SGBaseModel<DYMSDaiquerenModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.model = res.data
                
                switch self.model.orderStatus {
                case "NO_BUSINESS":
                    self.statusLb.text = "订单待确认"
                    self.shuomingLb.text = "商家正在休息中，营业后将优先处理您的订单，预计10:40左右短信告知订座结果"
                    self.yingyeImg.isHidden = false
                    self.chuliImg.isHidden = true
                    self.querenImg.isHidden = true
                case "PENDING":
                    self.statusLb.text = "订单处理中"
                    self.shuomingLb.text = "商家正在处理中，您将会收到短信告知订单结果"
                    self.yingyeImg.isHidden = false
                    self.chuliImg.isHidden = false
                    self.querenImg.isHidden = true
                case "RESULT":
                    self.statusLb.text = "预订成功"
                    self.shuomingLb.text = "餐厅已确认您的预订信息，请您按时到店就餐"
                    self.hideJingqu()
                case "CANCEL":
                    self.statusLb.text = "订单已取消"
                    self.shuomingLb.text = "商家正在处理中，您将会收到短信告知订单结果。"
                    self.hideJingqu()
                default:
                    break
                }
                self.tableView.reloadData()
            }
            
        } Error: {
            
        }
    }
    
    func hideJingqu() {
        self.yingyeImg.isHidden = true
        self.chuliImg.isHidden = true
        self.querenImg.isHidden = true
        self.lineView.isHidden = true
        self.yingye.isHidden = true
        self.chuli.isHidden = true
        self.queren.isHidden = true
    }
    
    func setupTopView() {
        view.addSubview(topBg)
        topBg.addSubview(statusLb)
        topBg.addSubview(shuomingLb)
        topBg.addSubview(lineView)
        
        topBg.addSubview(yingye)
        topBg.addSubview(chuli)
        topBg.addSubview(queren)
        
        topBg.addSubview(yingyeBg)
        topBg.addSubview(chuliBg)
        topBg.addSubview(querenBg)
        
        topBg.addSubview(yingyeImg)
        topBg.addSubview(chuliImg)
        topBg.addSubview(querenImg)
        
        topBg.addSubview(back)
        back.addAction {[weak self] in
            if let `self` = self {
                self.popVC()
            }
        }
        self.backSnp(back)
        topBg.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(274.autoWidth())
        }
        statusLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(19.autoWidth() + NAV_HEIGHT)
            make.left.equalToSuperview().offset(16.autoWidth())
            make.height.equalTo(18.autoWidth())
        }
        shuomingLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(49.autoWidth() + NAV_HEIGHT)
            make.left.equalToSuperview().offset(16.autoWidth())
            make.right.equalToSuperview().offset(-130.autoWidth())
        }
        yingye.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(113.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(-128.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        chuli.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(113.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview()
            make.height.equalTo(13.autoWidth())
        }
        queren.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(113.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(128.autoWidth())
            make.height.equalTo(13.autoWidth())
        }
        yingyeBg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(94.5.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(-128.autoWidth())
            make.width.height.equalTo(12.autoWidth())
        }
        chuliBg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(94.5.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(12.autoWidth())
        }
        querenBg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(94.5.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(128.autoWidth())
            make.width.height.equalTo(12.autoWidth())
        }
        yingyeImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(96.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(-128.autoWidth())
            make.width.height.equalTo(9.autoWidth())
        }
        chuliImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(96.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(9.autoWidth())
        }
        querenImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(96.autoWidth() + NAV_HEIGHT)
            make.centerX.equalToSuperview().offset(128.autoWidth())
            make.width.height.equalTo(9.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(100.autoWidth() + NAV_HEIGHT)
            make.left.equalToSuperview().offset(60.autoWidth())
            make.right.equalToSuperview().offset(-60.autoWidth())
            make.height.equalTo(2.autoWidth())
        }
        
    }

    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 15.autoWidth(), y: NAV_HEIGHT + 145.autoWidth(), width: SCREEN_WIDTH-30.autoWidth(), height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT-145.autoWidth() ), style:.grouped)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = .clear
        self.tableView!.register(DYJQWentiCell.self, forCellReuseIdentifier: "DYJQWentiCell")
        self.tableView!.register(DYMSDingdanInfoCell.self, forCellReuseIdentifier: "DYMSDingdanInfoCell")
        
        self.view.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 1{
            
            let cell: DYJQWentiCell? = tableView.dequeueReusableCell(withIdentifier: "DYJQWentiCell") as? DYJQWentiCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                cell?.configure(model)
            }
            
            return cell!
        } else {
            
            let cell: DYMSDingdanInfoCell? = tableView.dequeueReusableCell(withIdentifier: "DYMSDingdanInfoCell") as? DYMSDingdanInfoCell
            cell?.selectionStyle = .none
            cell?.currentVC = self
            if nil != self.model {
                
                cell?.configure(model)
            }
            
            return cell!
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let v = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
        return v
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
        return v
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
}
